package com.kikt.cutpicdemo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.kikt.commonutils.CommonUtils;
import com.kikt.commonutils.ToastUtils;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class MainActivity extends AppCompatActivity {

    private static final int CROP = 3;

    @Bind(R.id.bt_camera)
    Button mBtCamera;
    protected Uri uri;

    @OnClick(R.id.bt_camera)
    public void camera(View v) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String imgUrlFromTime = CommonUtils.getImgUrlFromTime();
        uri = Uri.fromFile(new File(imgUrlFromTime));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, CAMERA);
    }

    @Bind(R.id.bt_local)
    Button mBtLocal;

    @OnClick(R.id.bt_local)
    public void local(View v) {
        ToastUtils.toast("local");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ToastUtils.init(getApplication());
        ButterKnife.bind(this);//一个注解注入的控件
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case CAMERA:
                crop(uri);
                break;
            case LOCAL:
                break;
        }
    }

    private void crop(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setClassName("com.android.camera", "com.android.camera.CropImage");
        intent.setData(uri);
        intent.putExtra("outputX", 100);
        intent.putExtra("outputY", 100);
        intent.putExtra("aspectX", 120);
        intent.putExtra("aspectY", 120);
        intent.putExtra("scale", true);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("output", Uri.fromFile(new File(CommonUtils.getImgUrlFromTime())));
        startActivityForResult(intent, CROP);
    }

    private void openActivity(Bitmap bm) {
        PostBitmapEvent event = new PostBitmapEvent();
        event.bitmap = bm;

        //粘性的EventBus用法，可以用于组件间的对象的传递
        EventBus.getDefault().postSticky(event);

        Intent intent = new Intent(this, CropImageActivity.class);
        startActivity(intent);
    }

    public static final int CAMERA = 1;
    public static final int LOCAL = 2;
}
